package com.ruoyi.pos.config;


import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 多数据源配置
 * 
 * 配置主数据源（POS系统）和第三方数据源（会员、商品系统）
 * 
 * @author POS System
 * @since 1.0.0
 */
@Configuration
public class DataSourceConfig {

    // ==================== 主数据源（POS系统 - Oracle中文字符集） ====================

    /**
     * 主数据源配置
     * 用于POS系统的核心业务数据，使用Oracle中文字符集
     */
    @Primary
    @Bean(name = "posDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.pos")
    public DataSource posDataSource() {
        return new DruidDataSource();
    }

    /**
     * 主数据源SqlSessionFactory
     */
    @Primary
    @Bean(name = "posSqlSessionFactory")
    public SqlSessionFactory posSqlSessionFactory(@Qualifier("posDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/pos/*.xml"));
        return bean.getObject();
    }

    /**
     * 主数据源事务管理器
     */
    @Primary
    @Bean(name = "posTransactionManager")
    public DataSourceTransactionManager posTransactionManager(@Qualifier("posDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    // ==================== 会员数据源（第三方系统 - Oracle西欧字符集） ====================

    /**
     * 会员数据源配置
     * 用于连接第三方会员系统，使用Oracle西欧字符集
     */
    @Bean(name = "memberDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.member")
    public DataSource memberDataSource() {
        return new DruidDataSource();
    }

    /**
     * 会员数据源SqlSessionFactory
     */
    @Bean(name = "memberSqlSessionFactory")
    public SqlSessionFactory memberSqlSessionFactory(@Qualifier("memberDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/member/*.xml"));
        return bean.getObject();
    }

    /**
     * 会员数据源事务管理器
     */
    @Bean(name = "memberTransactionManager")
    public DataSourceTransactionManager memberTransactionManager(@Qualifier("memberDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    // ==================== 商品数据源（第三方系统 - Oracle西欧字符集） ====================

    /**
     * 商品数据源配置
     * 用于连接第三方商品系统，使用Oracle西欧字符集
     */
    @Bean(name = "goodsDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.goods")
    public DataSource goodsDataSource() {
        return new DruidDataSource();
    }

    /**
     * 商品数据源SqlSessionFactory
     */
    @Bean(name = "goodsSqlSessionFactory")
    public SqlSessionFactory goodsSqlSessionFactory(@Qualifier("goodsDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/goods/*.xml"));
        return bean.getObject();
    }

    /**
     * 商品数据源事务管理器
     */
    @Bean(name = "goodsTransactionManager")
    public DataSourceTransactionManager goodsTransactionManager(@Qualifier("goodsDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
